package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.MemberContactDao;
import cn.sumpu.app.guoguojie.domain.MemberContact;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class MemberContactDaoImpl extends SqlSessionDaoSupport implements
		MemberContactDao {
	private static Logger logger = LoggerFactory
			.getLogger(MemberContactDaoImpl.class);

	@Override
	public void insert(MemberContact contact) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.MemberContactMapper.insert",
							contact);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public void delContact(MemberContact contact) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.MemberContactMapper.delete",
							contact);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<MemberContact> getContactList(Map<String, Object> map)
			throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MemberContactMapper.getContactList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@Override
	public void updateContact(MemberContact contact)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.MemberContactMapper.updateContact",
							contact);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update contact failure.", e);
			}
			throw new AppBusinessException("update contact failure.", e);
		}

	}

}
